<template>
	<div class="box-right" v-loading="loading" element-loading-text="加载中" element-loading-background="rgba(0, 0, 0, 0.8)">
		<CommonModule name="客户类型">
			<template v-slot:header>
				<el-date-picker v-model="timeRange" value-format="yyyy-MM-dd" format="yyyy年M月d日" size="mini" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 16.875rem" :clearable="false"></el-date-picker>
			</template>
			<template v-slot:footer>
				<div class="data-board" id="customerType"></div>
			</template>
		</CommonModule>
	</div>
</template>

<script>
import CommonModule from "./CommonModule.vue"
import { getCustomerTypeData } from "@/api/home.js"
import { mixinDateRange } from "./mixin.js"

export default {
	components: { CommonModule },
	mixins: [mixinDateRange],
	data() {
		return {
			loading: false,
			customerType: null,
			objResizeObserver: null
		}
	},
	beforeDestroy() {
		this.objResizeObserver.disconnect()
	},
	methods: {
		async getData() {
			try {
				this.loading = true
				let res = await getCustomerTypeData({
					beginTime: this.timeRange[0],
					finishTime: this.timeRange[1]
				})
				// console.log(res)
				this.$nextTick(() => {
					this.drawBarChart(res.data)
				})
				this.loading = false
			} catch (error) {
				this.loading = false
				console.error("获取客户类型数据错误", error)
				this.$modal.msgError("获取客户类型数据错误")
			}
		},
		drawBarChart(data) {
			if (this.customerType) {
				this.customerType.dispose()
			}
			this.customerType = this.$echarts.init(document.getElementById("customerType"))
			let option = {
				tooltip: {
					trigger: "item",
					axisPointer: {
						type: "shadow"
					},
					backgroundColor: "rgba(2, 135, 147, 0.6)",
					textStyle: {
						color: "#fff"
					},
					formatter: "{b}：{d}%"
				},
				legend: {
					top: "center",
					orient: "vertical",
					right: "10%",
					itemWidth: 14,
					itemHeight: 14,
					itemGap: 20,
					textStyle: {
						color: "#fff",
						fontSize: 14,
						padding: [0, 0, 0, 10]
					},
					data: data.map(item => item.customerName)
				},
				grid: {
					top: "12%",
					left: "3%",
					right: "3%",
					bottom: "3%",
					containLabel: true
				},
				series: [
					{
						type: "pie",
						radius: ["40%", "60%"],
						center: ["40%", "50%"],
						color: ["#0074ff", "#01b4ff", "#2CAA3F", "#0260c8", "#8BCE1D", "#D170FF", "#F46D4F", "#FA9A09", "#FF317B", "#444444", "#BDBDBD", "#D01505", "#D5D5D5", "#D70303"],
						label: {
							position: "outside",
							show: true,
							color: "#fff",
							textBorderColor: "inherit",
							textBorderWidth: 1,
							fontSize: 12,
							formatter: function (params) {
								if (params.name !== "") {
									return params.percent + "%"
								}
							}
						},
						labelLine: {
							show: true,
							length2: 10,
							length: 10
						},
						data: data.map(item => {
							return {
								value: item.quantity,
								name: item.customerName
							}
						})
					}
				]
			}

			this.customerType.setOption(option)
			this.objResizeObserver = new ResizeObserver(() => {
				if (this.customerType) {
					this.customerType.resize()
				}
			})
			this.objResizeObserver.observe(document.getElementById("customerType"))
			this.handleChartLoop(option, this.customerType)
		},
		// 饼图自动轮播
		handleChartLoop(option, myChart) {
			if (!myChart) {
				return
			}
			let currentIndex = -1 // 当前高亮图形在饼图数据中的下标
			//     // 显示 tooltip

			let changePieInterval = setInterval(selectPie, 4000) // 设置自动切换高亮图形的定时器

			// 取消所有高亮并高亮当前图形
			function highlightPie() {
				// 遍历饼图数据，取消所有图形的高亮效果
				for (var idx in option.series[0].data) {
					myChart.dispatchAction({
						type: "downplay",
						seriesIndex: 0,
						dataIndex: idx
					})
				}
				// 高亮当前图形
				myChart.dispatchAction({
					type: "highlight",
					seriesIndex: 0,
					dataIndex: currentIndex
				})
			}

			// 用户鼠标悬浮到某一图形时，停止自动切换并高亮鼠标悬浮的图形
			myChart.on("mouseover", params => {
				clearInterval(changePieInterval)
				currentIndex = params.dataIndex
				highlightPie()
			})

			// 用户鼠标移出时，重新开始自动切换
			myChart.on("mouseout", params => {
				if (changePieInterval) {
					clearInterval(changePieInterval)
				}
				changePieInterval = setInterval(selectPie, 1000)
			})

			// 高亮效果切换到下一个图形
			function selectPie() {
				var dataLen = option.series[0].data.length
				currentIndex = (currentIndex + 1) % dataLen
				highlightPie()
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.box-right {
	width: 59.5%;
	height: 100%;
	display: flex;
	flex-direction: column;
	box-sizing: border-box;
	flex-shrink: 0;
}

.data-board {
	width: 100%;
	height: 100%;
	overflow: hidden;
}
</style>
